<template>
  <div class="w-full h-full bgMeau">
    <Nav class="fixed top-0 w-full z-10" />
    <div class="mt-10 bodyPanel">
      <van-empty v-if="panelData.length == 0" description="此用户无权限" image-size="10rem" />
      <MenuList v-else :panel-data="panelData" @clickitem="clickItem" />
    </div>
  </div>
</template>

<script>
  import { defineComponent, reactive, toRefs, computed } from 'vue'
  import isEmpty from 'lodash/isEmpty'
  import { useRouter, useRoute } from 'vue-router'
  import { useStore } from 'vuex'
  import Nav from '../components/nav'
  import MenuList from '../components/MenuList'
  import { Empty } from 'vant'
  import { damsPost } from '../api/api.js'
  export default defineComponent({
    name: 'MenuPage',
    components: { MenuList, Nav, [Empty.name]: Empty },
    setup() {
      let Panel = reactive({
        panelData: [],
      })
      const store = useStore()
      // 进入页面时清除之前打开页面的记录 防止多页面同时存在影响当前页面的判断（会加载多余的页面json）
      store.dispatch('tabs/delAllVisitedRoutes')
      store.dispatch('preview/initPages')
      // 自定义导航栏admin
      store.dispatch('pdaNav/setnavData', {
        title: '功能面板',
        outLogin: true,
        notMenu: true,
      })

      //web端跳转过来获取新的token的接口

      const webGetTk = async () => {
        let method = 'sysUserServiceImpl.loginForPda'
        const res = await damsPost({ LOGIN_ENTRANCE: 'PDA' }, method)
        console.log('res---webGetTk', res)
        if (isEmpty(res)) {
          router.push('/')
        } else {
          // userStore.setAppList(res.data)
          this.$store.dispatch('user/setToken', res.data.token)
          console.log('王月琪打印请求参数,getUserInfo', store.getters['user/businessCode'])
          await store.dispatch('user/getUserInfo', {
            appId: '0238eeb7d5c44c7b9b57f7397eb363cf',
            appCode: 'WMS',
            language: 'zh_CN',
            datas: res.data,
          })
          // await getUserInfo()
          await store.dispatch('routes/setRoutes', 'none')
          // const { setRoutes } = useRouterStore()
          // setRoutes()
          setTimeout(() => {
            init()
          }, 1)
        }
      }
      const route = useRoute()
      if (route.query.webTk) {
        // web端跳转过来的
        webGetTk()
      } else {
        init()
      }

      //初始化
      function init() {
        const routes = computed(() => store.getters['routes/routes'])

        let _routers = routes.value.filter((v) => {
          return v.meta && !v.meta.hidden && v.name !== 'Dashboard'
        })
        // _routers.forEach((v) => {
        //   if (!isEmpty(v.children)) {
        //     for (let vv of v.children) {
        //       vv.path = `${v.path}/${vv.path}`
        //     }
        //   }
        // })
        console.log('routes', _routers)
        Panel.panelData = _routers
        // 重置所有界面数据
        // const storePages = usePagesStore()
        // storePages.resetPages()
        //暂时没发现啥作用 先隐藏 bobby
      }

      // 事件
      const router = useRouter()
      function clickItem(item) {
        router.push(`${item.path}`)
      }

      return {
        ...toRefs(Panel),
        clickItem,
        init,
      }
    },
  })
</script>

<style lang="scss">
  .bgMeau {
    background: #fff !important;
  }
  .bodyPanel {
    margin-top: 3rem !important;
    border: 1px solid #fff !important;
    background: #fff !important;
  }
</style>
